<?php
class profile{

    var $page;
    var $userid;
    var $username = null;
    var $avatar = null;
    var $name = null;
    var $realname = null;
    var $surname = null;
    var $joindate = null;
    var $profession = null;
    var $os = null;
    var $commentCount = null;

    function set_page($new_page){
        $this->pageid = $new_page;
        return $this->pageid;
    }

    function set_userid($new_user){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT username FROM ".$prefix."users WHERE id = '".$new_user."'");
        $row = $query->fetch_object();
        $this->user = $row->username;
        return $this->user;
        $query->close();
        $connect->close();
    }

    function get_username(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT username FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        $this->username = $row->username;
        return $this->username;
        $query->close();
        $connect->close();
    }

    function get_name(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT username FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        if($row->username != $_SESSION['username']){
            $this->name = $row->username."'s";
        }
        else{
            $this->name = "Your";
        }
        return $this->name;
        $query->close();
        $connect->close();
    }

    function get_userfolder(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT avatar FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        if(empty($row->avatar)){
            $this->userfolder = "default";
        }
        elseif(!empty($row->avatar)){
            $this->userfolder = strtolower($this->user);
        }
        return $this->userfolder;
        $query->close();
        $connect->close();
    }

    function get_avatar(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT avatar FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        if(empty($row->avatar)){
            $this->avatar = "nopicture.gif";
            //$nopicture = "It seems you do not have an Avatar specified, would you like to do that right now?";
        }
        else{
            $this->avatar = strtolower($row->avatar);
        }
        return $this->avatar;
        $query->close();
        $connect->close();
    }

    function get_avatarComment(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT img_comment FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        $this->avatarComment = $row->img_comment;
        return $this->avatarComment;
        $query->close();
        $connect->close();
    }

    function get_realname(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT name FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        $this->realname = $row->name;
        return $this->realname;
        $query->close();
        $connect->close();
    }

    function get_surname(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT surname FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        $this->surname = $row->surname;
        return $this->surname;
        $query->close();
        $connect->close();
    }

    function get_joinDate(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT joined FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        $this->joindate = $row->joined;
        return $this->joindate;
        $query->close();
        $connect->close();
    }

    function get_profession(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT profession FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        $this->profession = $row->profession;
        return $this->profession;
        $query->close();
        $connect->close();
    }

    function get_os(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT os FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        $this->os = $row->os;
        return $this->os;
        $query->close();
        $connect->close();
    }

    function get_osList(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT osname FROM ".$prefix."os GROUP BY osname");
        while($row = $query->fetch_object()){
            if($row->osname != $this->os){
                $this->osList[] = array(
                "osname" => $row->osname
                );
            }
        }
        return $this->osList;
        $query->close();
        $connect->close();
    }

    function get_osSummary(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT summary FROM ".$prefix."os WHERE  osname = '".$this->os."'");
        $row = $query->fetch_object();
        $this->ossummary = $row->summary;
        return $this->ossummary;
        $query->close();
        $connect->close();
    }

    function get_commentCount(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT id FROM ".$prefix."comments WHERE author = '".$this->user."'");
        $numrows = $query->num_rows;
        $this->commentCount = $numrows;
        return $this->commentCount;
        $query->close();
        $connect->close();
    }

    function get_commentPercentage(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT id FROM ".$prefix."comments");
        $numrows = $query->num_rows;
        $this->percentage = $this->commentCount / $numrows * 100;
        return $this->percentage;
        $query->close();
        $connect->close();
    }

    function get_aboutMe(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT aboutme FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        $this->aboutMe = $row->aboutme;
        return $this->aboutMe;
        $query->close();
        $connect->close();
    }

    function get_onMind(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT onmind FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        $this->onMind = $row->onmind;
        return $this->onMind;
        $query->close();
        $connect->close();
    }


    function get_privacy(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT private FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        $this->privacy = $row->private;
        return $this->privacy;
        $query->close();
        $connect->close();
    }

    function checked(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT private FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();

        if($row->private == "1"){
            $this->checked = "checked";
        }
        return $this->checked;
        $query->close();
        $connect->close();
    }

    function get_feed(){
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT feed FROM ".$prefix."users WHERE username = '".$this->user."'");
        $row = $query->fetch_object();
        $this->feed = $row->feed;
        return $this->feed;
        $query->close();
        $connect->close();
    }
}

class Listfiles{

    var $dir= '.';
    var $filter = false;
    var $filetype = array();
    var $files = array();

    function Listfiles($ext=false){
        $args = func_get_args();
        $this->filter = (count($args))?true: false;
        if($this->filter){
            foreach($args as $e){
                array_push($this->filetype, $e);
            }
        }
        return($this->filetype);
    }
    function setDir($dir = false){
        $this->dir = trim($dir);
        if(is_dir($this->dir)){
            return true;
        }
        return false;
    }
    function getFiles(){
        $handle = @opendir($this->dir);
        if($handle){
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    if(is_file($this->dir . "/" . $file)){
                        $fileinfo = pathinfo($this->dir . "/" . $file);
                        foreach($this->filetype as $type){
                            if($type == $fileinfo['extension']){
                                array_push($this->files,$file);
                            }
                        }
                    }
                }
            }
            closedir($handle);
            return($this->files);
        } else {
            return ("error: Not a valid directory resource: " . $this->dir);
        }
    }
} 
?>
